# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  
@software: PyCharm 
@file: class_collections1.py 
@time: 2019/6/25 10:14
"""
'''
自定义类继承collections.MutableSequence(可变序列)
'''
import collections

class Items(collections.MutableSequence):
    def __init__(self, initial=None):
        self._items = list(initial) if initial is not None else []

    # Required sequence methods
    def __getitem__(self, item):
        print('Getting:', item)
        return self._items[item]

    def __setitem__(self, key, value):
        print('Setting:', key, value)
        self._items[key] = value

    def __delitem__(self, key):
        print('Deleting:', key)
        del self._items[key]

    def insert(self, index, value):
        print('Inserting:index {{{}}}, value {{{}}}'.format(index, value))
        self._items.insert(index, value)

    def __len__(self):
        print('Len')
        return len(self._items)


# Example use
a = Items([1, 2, 3])
print(len(a))
a.append(4)
print(a.__dict__)
a.append(2)
print(vars(a))
print(a.count(2)) # 统计“2”出现的次数是两次
a.remove(3)
print(vars(a))
a.insert(0, 'a')
print(vars(a))
a.remove('a')
print(vars(a))